package com.example.dell.fastshopping.views;


import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.dell.fastshopping.R;

/**
 * Created by i7-1 on 2018/11/22.
 */

public class InputPassword extends RelativeLayout {

    public MyEditText passwordEditText;

    public InputPassword(Context context) {
        super(context);

    }

    public InputPassword(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public InputPassword(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputPassword);
        String hint = ta.getString(R.styleable.InputPassword_hint);
        ta.recycle();
        RelativeLayout view = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.layout_password, this, false);
        addView(view);
        passwordEditText = view.findViewById(R.id.in_password);
        passwordEditText.setHint(hint);
        final ImageView buttom = view.findViewById(R.id.see_password);
        buttom.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (passwordEditText.getInputType() == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)) {
                    passwordEditText.setInputType((InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD));
                    buttom.setImageResource(R.mipmap.eyes_close);
                    passwordEditText.setSelection(passwordEditText.getText().toString().length());
                } else {
                    passwordEditText.setInputType((InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                    buttom.setImageResource(R.mipmap.eyes_open);
                    passwordEditText.setSelection(passwordEditText.getText().toString().length());
                }
            }
        });
    }

    public String getText() {
        return passwordEditText.getText().toString();
    }

    public void setText(String s) {
        passwordEditText.setText("");
    }
}
